﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hsmp.SocietyAdmin.IBusinessLogic;
using Hsmp.SocietyAdmin.BusinessLogic;
using Hsmp.SocietyAdmin.Entity;
using HSMPWeb.ViewEntity;

namespace HSMPWeb.UserControls
{
    public partial class FlatDetailsControl : System.Web.UI.UserControl
    {

        #region  Variable & constant
        string lCreatedByID = "SysAdmin";
        // Flat _Flat;
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckPageAuthorization())
            {
                string lQueryStringParameterFlatKey = Request.QueryString["FlatKey"];
                if (!string.IsNullOrEmpty(lQueryStringParameterFlatKey))
                {
                    if (!Page.IsPostBack)
                    {
                        Flat lFlat = new Flat();
                        lFlat = GetselectedFlat(lQueryStringParameterFlatKey);
                        FillScreen(lFlat);
                        Session["SelectedFlat"] = lFlat;
                       // this.lblFlatPageCaption.Text = "Edit Flat";
                        btnRegisterFlat.Visible = false;
                        btnUpdate.Visible = true;

                    }

                }
                else
                {
                    //this.lblFlatPageCaption.Text = "New Flat Registration";
                    btnRegisterFlat.Visible = true;

                }


                //Response.Redirect(Request.Url.AbsolutePath);
            }
            else
            {
                // Response.Redirect("~/Pages/AccessDenied.aspx");
            }
        }

        protected void btnRegisterFlat_Click(object sender, EventArgs e)
        {
            try
            {
                Flat lFlat = new Flat();

                lFlat.FlatSocietyKey = Convert.ToInt32(txtFlatSocietyId.Text);
                lFlat.FlatBlockName = this.txtFlatBlockName.Text;
                lFlat.FlatNumber = this.txtFlatNumber.Text;
                lFlat.FlatFloorNumber = this.txtFlatFloorNumber.Text;
                lFlat.FlatType = this.txtFlatType.Text;
                lFlat.FlatNumberOfBedrooms = Convert.ToInt32(txtFlatNumberOfBedrooms.Text);
                lFlat.FlatNumberOfKitchen = Convert.ToInt32(txtFlatNumberOfKitchen.Text);
                lFlat.FlatNumberOfToilets = Convert.ToInt32(txtFlatNumberOfToilets.Text);
                lFlat.FlatNumberOfLivingroom = Convert.ToInt32(txtFlatNumberOfLivingroom.Text);
                lFlat.FlatNumberOfStudyroom = Convert.ToInt32(txtFlatNumberOfStudyroom.Text);
                lFlat.FlatNumberOfBalcony = Convert.ToInt32(txtFlatNumberOfBalcony.Text);
                lFlat.FlatTotalArea = Convert.ToInt32(txtFlatTotalArea.Text);
                lFlat.FlatNumberOfWaterInlet = Convert.ToInt32(txtFlatNumberOfWaterInlet.Text);
                lFlat.FlatOccupancy = Convert.ToInt32(dlsFlatOccupancy.SelectedItem.Value);
                lFlat.Sys_CreatedByID = lCreatedByID;



                FlatRegistrationBus lFlatRegistrationBus = new FlatRegistrationBus();
                lFlatRegistrationBus.RegisterNewFlat(lFlat);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                //Need to display the exception message to the user or log it in application log.
            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Flat lFlat = new Flat();
            lFlat = (Flat)Session["SelectedFlat"];
            lFlat.FlatSocietyKey = Convert.ToInt32(txtFlatSocietyId.Text);
            lFlat.FlatBlockName = this.txtFlatBlockName.Text;
            lFlat.FlatNumber = this.txtFlatNumber.Text;
            lFlat.FlatFloorNumber = this.txtFlatFloorNumber.Text;
            lFlat.FlatType = this.txtFlatType.Text;
            lFlat.FlatNumberOfBedrooms = Convert.ToInt32(txtFlatNumberOfBedrooms.Text);
            lFlat.FlatNumberOfKitchen = Convert.ToInt32(txtFlatNumberOfKitchen.Text);
            lFlat.FlatNumberOfToilets = Convert.ToInt32(txtFlatNumberOfToilets.Text);
            lFlat.FlatNumberOfLivingroom = Convert.ToInt32(txtFlatNumberOfLivingroom.Text);
            lFlat.FlatNumberOfStudyroom = Convert.ToInt32(txtFlatNumberOfStudyroom.Text);
            lFlat.FlatNumberOfBalcony = Convert.ToInt32(txtFlatNumberOfBalcony.Text);
            lFlat.FlatTotalArea = Convert.ToInt32(txtFlatTotalArea.Text);
            lFlat.FlatNumberOfWaterInlet = Convert.ToInt32(txtFlatNumberOfWaterInlet.Text);
            lFlat.FlatOccupancy = Convert.ToInt32(dlsFlatOccupancy.SelectedItem.Value);
            lFlat.Sys_CreatedByID = lCreatedByID;

            FlatRegistrationBus lFlatRegistrationBus = new FlatRegistrationBus();
            lFlatRegistrationBus.UpdateRegisteredFlat(lFlat);

            //lblText.Text = "Successfully Updated";

        }
        #endregion Events

        #region Private Methods
        private bool CheckPageAuthorization()
        {
            bool IsUserAuthorized = false;
            if (Session["UserContext"] != null)
            {
                UserContext UserSession = new UserContext();
                UserSession = (UserContext)Session["UserContext"];
                lCreatedByID = UserSession.CurrentUser.UserLoginID;
                //if ((UserSession.UserRole != UserRoles.SocietySuperAdmin) || (UserSession.UserRole != UserRoles.SystemAdmin) || (UserSession.UserRole != UserRoles.SystemOperator) || (UserSession.UserRole != UserRoles.SocietySuperAdmin) || (UserSession.UserRole != UserRoles.SocietyAdmin) || (UserSession.UserRole != UserRoles.SocietyOperator))
                //{
                //    IsUserAuthorized = true;
                //    //Response.Redirect(Request.Url.AbsolutePath);
                //}
                //else
                //{
                // Response.Redirect("~/Pages/AccessDenied.aspx");
                //}
                IsUserAuthorized = true;
            }
            else
            {
                //Response.Redirect("~/Pages/AccessDenied.aspx");
            }
            return IsUserAuthorized;
        }
        private Flat GetselectedFlat(string FlatKey)
        {
            Flat lFlat;
            FlatRegistrationBus lFlatRegistrationBus = new FlatRegistrationBus();
            lFlat = lFlatRegistrationBus.GetSelectedFlatByFlatKey(FlatKey);
            return lFlat;
        }

        private void FillScreen(Flat pFlat)
        {
            this.lblFlatId.Text = pFlat.FlatKey.ToString();
            this.txtFlatSocietyId.Text = pFlat.FlatSocietyKey.ToString();
            this.txtFlatBlockName.Text = pFlat.FlatBlockName;
            this.txtFlatNumber.Text = pFlat.FlatNumber;
            this.txtFlatFloorNumber.Text = pFlat.FlatFloorNumber;
            this.txtFlatType.Text = pFlat.FlatType;
            this.txtFlatNumberOfBedrooms.Text = pFlat.FlatNumberOfBedrooms.ToString();
            this.txtFlatNumberOfKitchen.Text = pFlat.FlatNumberOfKitchen.ToString();
            this.txtFlatNumberOfToilets.Text = pFlat.FlatNumberOfToilets.ToString();
            this.txtFlatNumberOfLivingroom.Text = pFlat.FlatNumberOfLivingroom.ToString();
            this.txtFlatNumberOfStudyroom.Text = pFlat.FlatNumberOfStudyroom.ToString();
            this.txtFlatNumberOfBalcony.Text = pFlat.FlatNumberOfBalcony.ToString();
            this.txtFlatTotalArea.Text = pFlat.FlatTotalArea.ToString();
            this.txtFlatNumberOfWaterInlet.Text = pFlat.FlatNumberOfWaterInlet.ToString();
            this.dlsFlatOccupancy.Text = pFlat.FlatOccupancy.ToString();

        }
        #endregion Private Methods
        
    }
}